﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using RATSharpW7.ViewModel;
using System.Windows.Navigation;
using RATSharp.Model;

namespace RATSharpW7.View
{
    /// <summary>
    /// multiple reservation edit screen. Allows to change 
    /// more periods in one round.
    /// </summary>
    public partial class ReservationPeriods : PhoneApplicationPage
    {
        private static bool modelChanged { get; set;}

        /// <summary>
        /// this flag signals if model was changed by user recently. 
        /// because in that case during the stepback we want to omit edit reservation screen 
        /// and go directly to day view:
        /// 
        /// Day View -> Edit Reservation -> ReservationPeriods (model changed)
        ///                                                         |
        ///                                                         | (Stepback)
        ///                                                         |        
        ///                                                      Day View
        /// </summary>
        public static bool ModelChanged
        {
            get
            {
                bool old = modelChanged;
                modelChanged = false;
                return old;
            }
        }


        bool isNewPageInstance = false;
        ReservationPeriodsViewModel model;
        public ReservationPeriods()
        {
            InitializeComponent();
            isNewPageInstance = true;
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {

            if (e.NavigationMode !=
                  System.Windows.Navigation.NavigationMode.Back)
            {
                State["ReservationViewModel"] = model;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (isNewPageInstance)
            {
                if (model == null)
                {
                    if (State.ContainsKey("ReservationViewModel"))
                    {
                        model = State["ReservationViewModel"]
                                           as ReservationPeriodsViewModel;
                    }
                    else
                    {
                        var data = NavigationContext.GetData<period>();
                        model = new ReservationPeriodsViewModel();
                        model.loadPeriods(data.reservation.id.Value);
                    }
                }
                DataContext = model;
            }
            isNewPageInstance = false;
        }


        private void periodsListbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                // get RSS item from ListBox and store in class var. Store page to navigate to in class var.
                RATSharp.Model.period selectedItem = (RATSharp.Model.period)e.AddedItems[0];
                // reset selection of ListBox
                ((ListBox)sender).SelectedIndex = -1;

                selectedItem.Valid = !selectedItem.Valid;
            }
        }

        private void ApplicationSubmitButton_Click(object sender, EventArgs e)
        {
            model.saveChanges(new EventHandler(updateModelComplete));
        }

        private void updateModelComplete(object sender, EventArgs args)
        {
            if (NavigationService.CanGoBack)
            {
                modelChanged = true;
                NavigationService.GoBack();
            }
        }

    }
}